package com.freshfresh.activity.classification;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.freshfresh.activity.BaseActivity;
import com.freshfresh.activity.R;
import com.freshfresh.adapter.CommentAdapter;
import com.freshfresh.utils.ImageLoadOptions;
import com.freshfresh.utils.UrlConstants;
import com.freshfresh.utils.Utils;
import com.freshfresh.view.autoscroll.Imageloader_homePager;
import com.freshfresh.view.autoscroll.TagViewPager;
import com.freshfresh.view.autoscroll.TagViewPager.OnGetView;
import com.nostra13.universalimageloader.core.ImageLoader;
//评论列表
//评论列表
public class ProductCommentActivity extends BaseActivity {
	TagViewPager myviewpager;// banner
	FrameLayout query_frame;
	List<String> listmap_banner;
	List<Map<String,Object>> listmap_comment;
	Dialog progressDialog;
	CommentAdapter adapter;
	private ImageView iv_product_comment_national_flag=null;//国旗
	private TextView tv_ac_product_comment_name=null;//名称
	/**
	 * 上拉刷新的控件
	 */
	private ListView mPullRefreshListView;
	private TextView tv_title=null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ac_product_connemt);
		initView();
		initData();
		initBanner();
	}
	public void initData(){
		progressDialog=Utils.initProgressDialog(ProductCommentActivity.this);
		progressDialog.show();
		Map<String, String> params = new HashMap<String, String>();
		params.put("productid", "2341");
		params.put("page", "1");
		params.put("page_size", "100");
		params.put("sorttype", "");
		String ad_url = UrlConstants.product_comment_url;
		Log.e("url是多少…………", ad_url);
		executeRequest(new StringRequest(ad_url, params,
				new Response.Listener<String>() {
					public void onResponse(String arg0) {
						progressDialog.dismiss();
						Log.e("listmap的……", arg0);
						if (Utils.parseJsonStr(arg0).get("result").toString()
								.equals("1")) {
							Map<String,Object> map=(Map<String,Object>)Utils.parseJsonStr(arg0).get("data");
							listmap_comment=(List<Map<String,Object>>)map.get("review_list");
							adapter=new CommentAdapter(ProductCommentActivity.this, listmap_comment);
							mPullRefreshListView.setAdapter(adapter);
							
						} else {
							
						}
					}

				}, new Response.ErrorListener() {

					public void onErrorResponse(VolleyError arg0) {
						// dialog.dismiss();
						Toast.makeText(ProductCommentActivity.this, "获取失败", 1).show();
						progressDialog.dismiss();
					}
				}));
	
	}

	public void initView() {
		// 得到控件
		mPullRefreshListView = (ListView) findViewById(R.id.pull_refresh_list);
		tv_title = (TextView) findViewById(R.id.tv_title);
		tv_title.setText("顾客评价");
		
		myviewpager = (TagViewPager) findViewById(R.id.myviewpager);
		query_frame = (FrameLayout) findViewById(R.id.query_frame);
		iv_product_comment_national_flag = (ImageView) findViewById(R.id.iv_product_comment_national_flag);
		if(getIntent().getExtras()!=null){
			if(getIntent().getExtras().getString("national_flag")!=null){
				ImageLoader.getInstance().displayImage(getIntent().getExtras().getString("national_flag"), iv_product_comment_national_flag, ImageLoadOptions.getOptions(0));	
			}
		}
		tv_ac_product_comment_name = (TextView) findViewById(R.id.tv_ac_product_comment_name);
		if(getIntent().getExtras()!=null){
			if(getIntent().getExtras().getString("name")!=null){
				tv_ac_product_comment_name.setText(getIntent().getExtras().getString("name"));	
			}
		}
	}
	
		public void initBanner(){
			listmap_banner=(List<String>)getIntent().getSerializableExtra("list");
			Log.e("listmap_banner长度是多少…………", listmap_banner.size()+"");
			myviewpager.init(R.drawable.indicator_focused,
					R.drawable.indicator, 10, 5, 2, 20);
			myviewpager.setAutoNext(true, 2000);
			myviewpager.setOnGetView(new OnGetView() {

				@Override
				public View getView(ViewGroup container, int position) {
					// TODO Auto-generated method stub
					ImageView iv = new ImageView(ProductCommentActivity.this);
					iv.setScaleType(ScaleType.FIT_XY);
					Imageloader_homePager.displayImage(listmap_banner.get(position),iv, new Handler(), null);
					container.addView(iv);
					return iv;
				}
			});
			myviewpager.setAdapter(listmap_banner.size());
			// // 强制控件高度
			LinearLayout.LayoutParams titl = (LinearLayout.LayoutParams) query_frame.getLayoutParams(); // 取控件当前的布局参数
			titl.height = Utils.getScreenHeight(ProductCommentActivity.this)  / 3;// 控件的高强制设成
			query_frame.setLayoutParams(titl); // 使设置好的布局参数应用到控件
			
			FrameLayout.LayoutParams tit2 = (FrameLayout.LayoutParams) myviewpager.getLayoutParams(); // 取控件当前的布局参数
			tit2.height = Utils.getScreenHeight(ProductCommentActivity.this)  / 3;// 控件的高强制设成
			myviewpager.setLayoutParams(tit2); // 使设置好的布局参数应用到控件
		}
}
